<?php


namespace app\api\model;


use think\Db;

class Theme extends BaseModel
{

//    protected $hidden = ['update_time', 'delete_time', 'topic_img_id', 'head_img_id'];
//
//    public function topicImg()
//    {
//        return $this->belongsTo('Image', 'topic_img_id', 'id');
//
//    }
//
//    public function headImg()
//    {
//        return $this->belongsTo('Image', 'head_img_id', 'id');
//    }
//
//    public function products()
//    {
//        return $this->belongsToMany('Product', 'theme_product', 'product_id', 'theme_id');
//    }


//    public static function getThemeWithProducts($id)
//    {
//        $theme = self::with('products,topicImg,headImg')->find($id);
//        return $theme;
//    }


    public function topicImg()
    {
        return $this->belongsTo('Image', 'topic_img_id', 'id');
    }

    public function headImg()
    {
        return $this->belongsTo('Image', 'head_img_id', 'id');
    }

    /**
     * 关联product，多对多关系
     */
    public function products()
    {
        return $this->belongsToMany(
            'Product', 'theme_product', 'product_id', 'theme_id');
    }

    public static function getThemeWithProducts($id)
    {
        $themes = self::with('products,topicImg,headImg')
            ->find($id)->toArray();
        $themes['head_img'] = db('image')->where('id',$themes['head_img_id'])->find();
        $themes['head_img']['url'] = imgUrl($themes['head_img']['url'],$themes['head_img']);
        return $themes;
    }


    public static function getTheme($ids){
        $theme = self::with('topicImg,headImg')->select($ids);
        foreach ($theme as &$v){
            $v['topImg'] = db('image')->where('id',$v['topic_img_id'])->find();
            $v['topImg'] = imgUrl($v['topImg']['url'],$v['topImg']);
        }
        return $theme;
    }



    //获取theme数据
//    public function getTheme($ids){
//        $where['id']=array('in',$ids);
//        $theme = db('theme')->where($where)->field('id,name,description,topic_img_id,head_img_id')->select()->toArray();
//        foreach ($theme as &$value){
//            $value['topImg']  = db('image')->field('id,url,from')->where('id',$value['topic_img_id'])->find();
//            $value['topImg'] = $this->imgUrl($value['topImg']['url'],$value['topImg']);
//            $value['headerImg']  = db('image')->field('id,url,from')->where('id',$value['head_img_id'])->find();
//            $value['headerImg'] = $this->imgUrl($value['headerImg']['url'],$value['headerImg']);
//        }
//        return $theme;
//    }


    //获取theme下对应的商品信息

//    public  function getThemeWithProducts($id){
//        $theme = db('theme')->where('id',$id)->field('id,name,description,topic_img_id,head_img_id')->find();
//        $theme['head_url'] = db('image')->where('id',$theme['head_img_id'])->find();
//        $theme['head_url'] = $this->imgUrl($theme['head_url']['url'],$theme['head_url']);
//        $theme['product_ids'] = db('theme_product')->field('product_id')->where('theme_id',$id)->select()->toArray();
//        foreach ($theme['product_ids'] as $v){
//            $theme['product'][] = db('product')->field('id,name,price,stock,category_id,main_img_url,from,img_id')->where('id',$v['product_id'])->find();
//        }
//        unset($theme['product_ids']);
//        foreach ($theme['product'] as &$v){
//            $v['main_img_url'] = $this->imgUrl($v['main_img_url'],$v);
//        }
//        return $theme;
//    }


}


















